import { Response } from 'express';

export interface ApiResponse<T = any> {
  success: boolean;
  message?: string;
  data?: T;
  error?: string;
}

export class ResponseUtil {
  public static success<T>(res: Response, data?: T, message?: string, statusCode: number = 200): void {
    const response: ApiResponse<T> = {
      success: true,
      message,
      data
    };

    res.status(statusCode).json(response);
  }

  public static error(res: Response, message: string, statusCode: number = 500, error?: string): void {
    const response: ApiResponse = {
      success: false,
      message,
      error
    };

    res.status(statusCode).json(response);
  }

  public static notFound(res: Response, message: string = '资源不存在'): void {
    this.error(res, message, 404);
  }

  public static badRequest(res: Response, message: string = '请求错误'): void {
    this.error(res, message, 400);
  }

  public static unauthorized(res: Response, message: string = '未授权'): void {
    this.error(res, message, 401);
  }
}